home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 12 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  68 lines

  1. Path: dfw.nkn.net!usenet
  2. From: hattan@fastlane.net (John Hattan)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: RANDOM NUMBER GENERATOR
  5. Date: Sun, 31 Dec 1995 22:18:41 GMT
  6. Organization: The Code Zone
  7. Message-ID: <4c725d$4h5@dfw.nkn.net>
  8. References: <4bphpa$rc6@useneta1.news.prodigy.com> <4bpvu9$2p5@alterdial.uu.net> <4bs8mq$lcc@useneta1.news.prodigy.com>
  9. NNTP-Posting-Host: nfw54.fastlane.net
  10. X-Newsreader: Forte Agent .99b.112
  11.  
  12. BCNJ68B@prodigy.com (Tom Kellerman) wrote:
  13.  
  14. >crowleyj@approach.com (John (Jay) wrote:
  15. >
  16. >>Here's a C++ class that generates good random numbers.  I got the
  17. >>algorithm from MS Developer Network, which got it from a published
  18. >>article, which I can't seem to find now (probably Dr. Dobbs.).  From
  19. >>what I remember, the article indicated that it has a theoretical
  20. >>repetition period in the 100's of trillions of iterations.  Should be
  21. >>really easy to write your roll function using this.
  22. >>
  23. >> [random class carefully excised]
  24. >
  25. >Thank you very much for the Random Number Generator Class. I am only a 
  26. >novice C++ programmer so I need some help in how to know use this class 
  27. >to create a funcion that rolls two dice (return's a random number of 1-6, 
  28. >adds it to another rendom number of 1-6). I need to create a funcion 
  29. >rolldice() to do this. An example program of how I want to use this is to 
  30. >roll 2 dice say 100 times and count how many 7's were rolled. I need help 
  31. >in unsing that CLASS that you provided me to create my rolldice function. 
  32.  
  33. try this.  For more complicated stuff, a random number class is a good
  34. idea (for example, to get a random number divisible by a value and/or
  35. contained within a bound).  For something as simple as a die roll, just
  36. use the rand() and srand() functions.  As far as I know, they should
  37. exist in every C++ you can find. . .
  38.  
  39. main()
  40. {
  41.     srand(time());                      // seed the randomer so you
  42.                                         // don't always get the same
  43.                                         // stuff.
  44.     int number=0;
  45.     for (int i=1;i<=100;i++)
  46.     {
  47.         int result=rolldice();
  48.         if (result==7) number=number+1;
  49.     }
  50.     cout<<"The number of 7's rolled out of 100 rolls is"<<number;
  51. }
  52.  
  53. int rolldice(void)
  54. {
  55.     // rand() returns a big random number, mod it to get the bound you
  56.     // want.
  57.     int d1 = (rand()%6)+1;
  58.     int d2 = (rand()%6)+1;
  59.     return d1+d2;
  60. }
  61.  
  62. ---
  63. John Hattan          High UberPopeness -The First Church of Shatnerology
  64. The Code Zone        Sweet Software for a Saturnine World
  65. hattan@fastlane.net  http://www.fastlane.net/~hattan/
  66.  
  67.  
  68.